do (angular) ->
  angular.module 'tini'
  
  .factory 'req', ($rootScope, $http) ->
    broadcast = (msg) ->
      $rootScope.$broadcast msg

    # 增加查询串以提供对缓存的更新
    hash = '?' + (new Date).getTime().toString(36).substr 0, 3

    hash: hash

    # 请求文章索引
    index: (cb) ->
      $http.get 'assets/index.json' + hash
      .success (data) ->
        cb? data
      .error ->
        broadcast 'errIndexNotFound'

    # 请求文章
    post: (post, cb) ->
      if cb && post?.path
        $http.get 'posts/' + post.path + hash
        .success (data) ->
          cb data
        .error ->
          broadcast 'errPostNotFound'
      else
        broadcast 'errPostNotFound'
